import { memoryConfig } from "../framework/modules/memoryConfig";
import Common from "../utils/Common";

/**
 * 自动赶山任务
 */
export class autoTaskModel {

    constructor(taskId: number, mapId: number) {
        this.taskId = taskId;
        this.mapId = mapId;
        this.time = Date.now();
        this.maxTime = 90 * 60 * 1000;
    }
    /**
     * 任务ID
     */
    public taskId: number = 0;
    /**
     * 地图ID
     */
    public mapId: number = 0;
    /**
     * 开始时间
     */
    public time: number = 0;
    /**
     * 总时间
     */
    public maxTime: number = 0;





    public static getRemainingTime(taskItem: autoTaskModel): number {
        var nowTime = Date.now();
        var execTime = (nowTime - taskItem.time);
        var remaining = taskItem.maxTime - execTime;
        if (remaining < 0)
            remaining = 0;
        return remaining;
    }

    public static getProgress(taskItem: autoTaskModel): number {
        let progress = (taskItem.maxTime - this.getRemainingTime(taskItem)) / taskItem.maxTime;
        if (progress > 1)
            progress = 1;
        return progress;
    }

    public static formatTime(taskItem: autoTaskModel): string {
        var remaining = this.getRemainingTime(taskItem);
        if (remaining <= 0)
            remaining = 0
        let hSize = (1000 * 60 * 60);
        let mSize = (1000 * 60);
        let sSize = (1000);
        let h = Math.floor(remaining / hSize);
        let m = Math.floor((remaining - h * hSize) / mSize);
        let s = Math.floor((remaining - h * hSize - m * mSize) / sSize);
        return `${Common.padLeft(`${h}`, 2, "0")}:${Common.padLeft(`${m}`, 2, "0")}:${Common.padLeft(`${s}`, 2, "0")}`;
    }
    /**
     * 产量
     * @param taskItem 
     * @returns 
     */
    public static getCrop(taskItem: autoTaskModel): number {
        let interval = memoryConfig.instance.getFloatKey("auto_task_interval", 300);
        let total = 0;
        if (taskItem) {
            //因该要产出的个数
            total = taskItem.maxTime / 1000 / interval;
        }
        return total;
    }
}